package cn.gotom.svg;

import android.graphics.Canvas;
import android.graphics.RectF;
import cn.gotom.svg.xml.StyleAttribute;

/**
 * @author 裴绍国
 * @author <a href="mailto:peixere@gmail.com">裴绍国</a>
 */
public class Ellipse extends RenderableElement
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	float cx = 0.0f;
	float cy = 0.0f;
	float rx = 0.0f;
	float ry = 0.0f;

	private RectF ellipse = new RectF();

	/** Creates a new instance of Rect */
	public Ellipse()
	{
	}

	protected void build() throws SVGException
	{
		super.build();

		StyleAttribute sty = new StyleAttribute();

		if (getAttribute(sty.setName("cx")))
			cx = sty.getFloatValueWithUnits();

		if (getAttribute(sty.setName("cy")))
			cy = sty.getFloatValueWithUnits();

		if (getAttribute(sty.setName("rx")))
			rx = sty.getFloatValueWithUnits();

		if (getAttribute(sty.setName("ry")))
			ry = sty.getFloatValueWithUnits();
		ellipse.set(cx - rx, cy - ry, cx + rx, cy + ry);
	}

	public void render(Canvas canvas) throws SVGException
	{
		if (this.visible())
		{
			beginLayer(canvas);
			renderEllipse(canvas);
			finishLayer(canvas);
		}
	}

	private void renderEllipse(Canvas canvas)
	{
		doFillPaint();
		canvas.drawOval(ellipse, paint);
		if (doStrokePaint())
			canvas.drawOval(ellipse, paint);
	}

	public RectF getBoundingBox()
	{
		return boundsToParent(includeStrokeInBounds(ellipse));
	}
}
